package com.example;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import ru.yandex.yandexmapkit.MapView;

import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * User: Barsik
 * Date: 07.10.12
 * Time: 22:19
 * To change this template use File | Settings | File Templates.
 */

public class FilterActivity extends Activity {

    ListView list;

    HashMap<String, HashMap<String, String>> GENRES_SETTINGS = new HashMap<String, HashMap<String, String>>();


    public void initGenres() {
        initGenresItem("luk", R.drawable.tnk, "Лукойл");
        initGenresItem("bp", R.drawable.bp_32x32x32, "BP");
        initGenresItem("tnk", R.drawable.tnk, "ТНК");
        initGenresItem("ros", R.drawable.tnk, "Роснефть");
        initGenresItem("gz", R.drawable.tnk, "Газнефть");
        initGenresItem("tat", R.drawable.tat, "Татнефть");
        initGenresItem("tat1", R.drawable.tat, "Татнефть1");
        initGenresItem("tat2", R.drawable.tat, "Татнефть2");
        initGenresItem("tat3", R.drawable.tat, "Татнефть3");
        initGenresItem("tat4", R.drawable.tat, "Татнефть4");
        initGenresItem("tat5", R.drawable.tat, "Татнефть5");
        initGenresItem("tat6", R.drawable.tat, "Татнефть6");
    }

    public void initGenresItem(String genre, Integer ico, String text) {
        HashMap<String, String> settings = new HashMap<String, String>();
        settings.put("ico", ico.toString());
        settings.put("text", text);
        GENRES_SETTINGS.put(genre, settings);
    }


    ItemsAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.filter);
        initGenres();
        list = (ListView) findViewById(R.id.azsListId);

        String[] genres = new String[GENRES_SETTINGS.size()];

        int i = 0;

        for (String key : GENRES_SETTINGS.keySet())
            genres[i++] = key;

//        list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        adapter = new ItemsAdapter(this, genres);
        list.setAdapter(adapter);

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {

                LinearLayout ll = (LinearLayout) arg1;
                CheckedTextView tv = (CheckedTextView) ll.findViewById(R.id.checkList);
                toggle(tv);
            }
        });
    }


    private class ItemsAdapter extends BaseAdapter {
        String[] items;

        public ItemsAdapter(Context context, String[] item) {
            this.items = item;
        }

        // @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.azsitem, null);
            }
            ImageView iv = (ImageView) v.findViewById(R.id.icon);
            CheckedTextView post = (CheckedTextView) v.findViewById(R.id.checkList);
            post.setText(GENRES_SETTINGS.get(items[position]).get("text"));
            iv.setImageResource(Integer.parseInt(GENRES_SETTINGS.get(items[position]).get("ico")));

            return v;
        }

        public int getCount() {
            return items.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }
    }

    public void toggle(CheckedTextView v) {
        if (v.isChecked()) {
            v.setChecked(false);
        } else {
            v.setChecked(true);
        }
    }

    public void switchTabInActivity(String indexTabToSwitchTo){
        MyActivity ParentActivity;
        ParentActivity = (MyActivity) this.getParent();
        ParentActivity.switchTab(indexTabToSwitchTo);
    }

    public  void  searchOnClick(View v){
        Toast.makeText(this, "Зачем вы нажали?", Toast.LENGTH_SHORT).show();
        switchTabInActivity("tab1");
        MapUtility.showObject((MapView) v.findViewById(R.id.map));
    }
}